/**
 * 
 */
package com.wsd.service.impl;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.util.Set;

import com.wsd.bean.OrderList;
import com.wsd.bean.Price;
import com.wsd.bean.ProductOrder;
import com.wsd.bean.Products;
import com.wsd.bean.Users;
import com.wsd.dao.OrderListDaoInterface;
import com.wsd.dao.ProductOrderDaoInterface;
import com.wsd.dao.ProductsDaoInterface;
import com.wsd.service.ProductOrderService;
import com.wsd.util.SendMail;

/**
 * @author TuZiJun
 *
 */
public class ProductOrderServiceImpl implements ProductOrderService {
	private ProductsDaoInterface productsDAO = null;
	private ProductOrderDaoInterface productOrderDAO = null;
	private OrderListDaoInterface orderListDAO = null;
	public void setOrderListDAO(OrderListDaoInterface orderListDAO) {
		this.orderListDAO = orderListDAO;
	}
	public void setProductsDAO(ProductsDaoInterface productsDAO) {
		this.productsDAO = productsDAO;
	}
	public void setProductOrderDAO(ProductOrderDaoInterface productOrderDAO) {
		this.productOrderDAO = productOrderDAO;
	}
	/* (non-Javadoc)
	 * @see com.wsd.service.UserService#addProduct(java.util.Set, int)
	 */
	@SuppressWarnings("unchecked")
	public Set<OrderList> saveProduct(Set<OrderList> orderlist,int pnum, int pid, int userLevel) {
		boolean ishas = false;
		OrderList ol = new OrderList();
		float productPrice = 0.0f;
		for (OrderList order : orderlist) {
			if(pid == order.getProducts().getPId()) {
				ishas = true;
				ol = order;
				pnum += order.getLNum();
				orderlist.remove(order);
				break;
			}
		}
		if (!ishas) {
			Products product = productsDAO.findById(pid);
			ol.setProducts(product);
			Set<Price> prices = product.getPrices();
			for(Price price : prices) {
				if(userLevel == price.getULevel()) {
					productPrice = price.getPrice();
					ol.setLPrice(productPrice);
					break;
				}
			}
		}
		ol.setLNum(pnum);
		ol.setLSumPrice(ol.getLPrice() * ol.getLNum());
		orderlist.add(ol);
		return orderlist;
	}
	/* (non-Javadoc)
	 * @see com.wsd.service.UserService#deleteProduct(java.util.Set, int)
	 */
	public Set<OrderList> deleteProduct(Set<OrderList> orderlist, int pid) {
		for (OrderList order : orderlist) {
			if(pid == order.getProducts().getPId()) {
				orderlist.remove(order);
				break;
			}
		}
		return orderlist;
	}
	/* (non-Javadoc)
	 * @see com.wsd.service.UserService#saveProductOrder(java.util.Set, com.wsd.bean.Users)
	 */
	public ProductOrder saveProductOrder(Set<OrderList> orderlist, Users user, String ps) {
		int sum = 0;
		float sumPrice = 0.0f;
		ProductOrder po = new ProductOrder();
		for (OrderList order : orderlist) {
			sum += order.getLNum();
			sumPrice += order.getLSumPrice();
		}
		po.setOTime(new Timestamp(System.currentTimeMillis()));
		po.setOState(false);
		po.setOSumNum(sum);
		po.setOSumPrice(sumPrice);
		po.setUsers(user);
		po.setOrderLists(orderlist);
		productOrderDAO.save(po);
		String subject = user.getUName() + "--" + user.getCompany() + "--订单" + po.getOId() + "--" + DateFormat.getDateTimeInstance().format(po.getOTime());
		SendMail.send(subject, getContent(po, ps));
		for (OrderList order : orderlist) {
			order.setProductOrder(po);
			orderListDAO.save(order);
		}
		return po;
	}
	
	@SuppressWarnings("unchecked")
	public String getContent(ProductOrder order, String ps) {
		StringBuilder sb = new StringBuilder();
		Users user = order.getUsers();
		sb.append("<table width='1000' border='1px' border-color='#000' border-style='solid' cellspacing='0' cellpadding='0' align='left'>");
		sb.append("<tr class='texthei' align='left'><th colspan='8'>");
		sb.append("订单" + order.getOId() + "明细&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + DateFormat.getDateTimeInstance().format(order.getOTime()) + "</th></tr>");
		sb.append("<tr class='texthei' align='center'><th colspan='8'>");
		sb.append(user.getCompany());
		sb.append("</th></tr>");
		sb.append("<tr class='texthei' align='center'>");
		sb.append("<th>产品编号</th>");
		sb.append("<th>产品名称</th>");
		sb.append("<th>产品厂商</th>");
		sb.append("<th>产品型号</th>");
		sb.append("<th>产品条码</th>");
		sb.append("<th>产品数量</th>");
		sb.append("<th>产品单价</th>");
		sb.append("<th>产品总价</th></tr>");
		for (OrderList ol : (Set<OrderList>)order.getOrderLists()) {
			sb.append("<tr class='texthei' align='center'>");
			sb.append("<td>" + ol.getProducts().getPId() + "</td>");
			sb.append("<td>" + ol.getProducts().getPName() + "</td>");
			sb.append("<td>" + ol.getProducts().getFactory() + "</td>");
			sb.append("<td>" + ol.getProducts().getModel() + "</td>");
			sb.append("<td>" + ol.getProducts().getBarcode() + "</td>");
			sb.append("<td>" + ol.getLNum() + "</td>");
			sb.append("<td>￥" + ol.getLPrice() + "元</td>");
			sb.append("<td>￥" + ol.getLSumPrice() + "元</td></tr>");
		}
		sb.append("<tr><td colspan='8' align='right'>");
		sb.append("产品总量：" + order.getOSumNum() + "&nbsp;&nbsp;&nbsp;&nbsp;");
		sb.append("产品总价：￥" + order.getOSumPrice() + "元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		sb.append("</td></tr><tr><td colspan='8' align='right'>送货人：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		sb.append("联系人：" + user.getUName() + "&nbsp;&nbsp;&nbsp;联系电话：" + user.getMobile() + "&nbsp;&nbsp;&nbsp;" + user.getPhone() + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		sb.append("</td></tr><tr><td colspan='8' style='color:#f00'>");
		sb.append("备注：" + ps);
		sb.append("</td></tr></table>");
		sb.trimToSize();
		return sb.toString();
	}
}
